package com.itic.ht.cms.examrecord.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.cms.exam.persistence.dao.ExamDao;
import com.itic.ht.cms.exam.persistence.model.Exam;
import com.itic.ht.cms.exam.persistence.model.ExamExample;
import com.itic.ht.cms.examrecord.persistence.dao.ExamRecordDao;
import com.itic.ht.cms.examrecord.persistence.model.ExamRecord;
import com.itic.ht.cms.examrecord.persistence.model.ExamRecordExample;
import com.itic.ht.cms.examrecord.persistence.model.ExamRecordExample.Criteria;
import com.itic.ht.cms.studyrecord.persistence.model.StudyRecord;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;

@Service
@Transactional
public class ExamRecordService {
	// 合格分数比例
	private static final double EXAM_PROPORTION = 0.85;
	// 每道题的分数
	private static final int EXAM_QUESTION_SCORE = 4;

	protected static final String SUCCESS = ResponseCode.SUCCESS.getCode();
	protected static final String FAILED = ResponseCode.SERVICE_ERROR.getCode();

	@Autowired
	private ExamRecordDao mapper;
	@Autowired
	private ExamDao examMapper;
	@Autowired
	private WebMemberService webMemberService;

	public int count(ExamRecordExample example) {
		return mapper.countByExample(example);
	}

	public List<ExamRecord> list(ExamRecordExample example) {
		return mapper.selectByExample(example);
	}

	public ExamRecord get(String id) {
		if (StringHelper.isNotEmpty(id)) {
			return mapper.selectByPrimaryKey(id);
		}
		return null;
	}

	public String add(ExamRecord templet) {
		int code = 0;
		if (null != templet) {
			String progress = DBConst.TRUE;
			templet.setExamResult(DBConst.FALSE);
			// 获取当前考试进度x
			ExamRecordExample example = new ExamRecordExample();
			example.createCriteria().andDelFlagEqualTo(Integer.parseInt(DBConst.TRUE)).andAccountEqualTo(templet.getAccount());
			List<ExamRecord> list = mapper.selectByExample(example);
			if(null!=list&&list.size()>0){
				ExamRecord examRecord = list.get(0);
				if(null!=examRecord){
					progress = examRecord.getExamProgress();
				}
			}
			// 获取当前考试成绩
			Integer memPoints = templet.getMemPoints();
			// 获取试卷的题目数量（默认每题4分）
			String examId = templet.getExamId();
			Exam examModel = examMapper.selectByPrimaryKey(examId);
			Integer num = 25;
			String categoryId = "";
			if (null != examModel) {
				//num = examModel.getExecuteNum();
				categoryId = examModel.getCategoryId();
			}
			templet.setCategoryId(categoryId);
			// 查询当前考试总分数
			Integer sumPoints = num * EXAM_QUESTION_SCORE;
			// 判断当前进度是否完成（成绩大于总分百分之八十五）
			templet.setExamProgress(progress);
			if (memPoints >= sumPoints * EXAM_PROPORTION) {
				// 考试完成 进度加一
				templet.setExamProgress(Integer.parseInt(progress) + 1 + "");
				templet.setExamResult(DBConst.TRUE);
			}
			// 考试未完成 进度不变
			
			templet.setCtime(new Date());
			templet.setDelFlag(Integer.parseInt(DBConst.TRUE));
			templet.setMtime(new Date());
			templet.setSumPoints(sumPoints);
			templet.setId(IdGen.uuid());
			if (StringHelper.isBlank(templet.getRealName())) {
				// 查询用户信息
				WebMember webMember = webMemberService.getMemberByAccount(templet.getAccount());
				if (null != webMember) {
					templet.setRealName(webMember.getRealname());
				}
			}
		}
		code = mapper.insert(templet);
		if (code > 0) {
			return SUCCESS;
		} else {
			return FAILED;
		}
	}

	public String delete(ExamRecord model) {
		int code = 0;
        String id = model.getId();
        if (StringHelper.isNotEmpty(id.toString().trim())) {
            if (null == model.getMtime()) {
                model.setMtime(new Date());
            }
            model.setDelFlag(Integer.parseInt(DBConst.FALSE));
            code = mapper.updateByPrimaryKeySelective(model);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }	
	}

	public List<ExamRecord> getExamRecordByExample(ExamRecordExample example) {
		return mapper.selectByExample(example);
	}
}
